package com.southwind.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.southwind.entity.ProductCategory;
import com.southwind.entity.ProductInfo;
import com.southwind.exception.ShopException;
import com.southwind.result.ResponseEnum;
import com.southwind.service.ProductCategoryService;
import com.southwind.service.ProductInfoService;
import com.southwind.util.ResultVOUtil;
import com.southwind.vo.BuyerProductCategoryVO;
import com.southwind.vo.BuyerProductInfoVO;
import com.southwind.vo.ResultVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 类目表 前端控制器
 * </p>
 *
 * @author southwind
 * @since 2023-12-27
 */
@RestController
@RequestMapping("/buyer/product")
public class BuyerProductController {


    @Autowired
    private ProductCategoryService productCategoryService;
    @Autowired
    private ProductInfoService productInfoService;

    /**
     * 返回分类以及分类下有什么商品
     *
     * @return
     */
    @GetMapping("/list")
    public ResultVO list() {

        // 查询有哪些分类
        List<ProductCategory> productCategoryList = productCategoryService.list();

        List<BuyerProductCategoryVO> productCategoryVOList = new ArrayList<>();
        for (ProductCategory category : productCategoryList) {
            BuyerProductCategoryVO buyerProductCategoryVO = new BuyerProductCategoryVO();
            buyerProductCategoryVO.setName(category.getCategoryName());
            buyerProductCategoryVO.setType(category.getCategoryType());
            //分类下面的商品list集合，根据category_type查询
            QueryWrapper<ProductInfo> queryWrapper = new QueryWrapper();
            queryWrapper.eq("category_type", category.getCategoryType());
            List<ProductInfo> productInfoList = productInfoService.list(queryWrapper);

            List<BuyerProductInfoVO> buyerProductInfoVOList = new ArrayList<>();
            // 组装vo对象
            for (ProductInfo productInfo : productInfoList) {
                BuyerProductInfoVO buyerProductInfoVO = new BuyerProductInfoVO();
                BeanUtils.copyProperties(productInfo, buyerProductInfoVO);
                buyerProductInfoVOList.add(buyerProductInfoVO);
            }
            buyerProductCategoryVO.setGoods(buyerProductInfoVOList);
            // 这你这个productCategoryList，一边遍历一边add java.util.ConcurrentModificationException
            // 并发修改异常了 不能遍历的同时，对集合中的数据操作
            //     productCategoryList.add(category);
            productCategoryVOList.add(buyerProductCategoryVO);

        }


        return ResultVOUtil.success(productCategoryVOList);

//        List<ProductCategory> productCategoryList = this.productCategoryService.list();
//        List<BuyerProductCategoryVO> list = new ArrayList<>();
//        for (ProductCategory productCategory : productCategoryList) {
//            BuyerProductCategoryVO buyerProductCategoryVO = new BuyerProductCategoryVO();
//            buyerProductCategoryVO.setName(productCategory.getCategoryName());
//            buyerProductCategoryVO.setType(productCategory.getCategoryType());
//            QueryWrapper<ProductInfo> queryWrapper = new QueryWrapper<>();
//            queryWrapper.eq("category_type", productCategory.getCategoryType());
//            List<ProductInfo> productInfoList = this.productInfoService.list(queryWrapper);
//            List<BuyerProductInfoVO> list1 = new ArrayList<>();
//            for (ProductInfo productInfo : productInfoList) {
//                BuyerProductInfoVO vo = new BuyerProductInfoVO();
//                BeanUtils.copyProperties(productInfo, vo);
//                list1.add(vo);
//            }
//            buyerProductCategoryVO.setGoods(list1);
//            list.add(buyerProductCategoryVO);
//        }
//        return ResultVOUtil.success(list);
    }


    /**
     * 根据商品id查询价格
     *
     * @param id 商品id
     * @return
     */
    @GetMapping("/findPriceById/{id}")
    public BigDecimal findPriceById(@PathVariable("id")Integer id) {
        ProductInfo productInfo = productInfoService.getById(id);
        if (productInfo == null) {
            throw new ShopException(ResponseEnum.PRODUCT_NULL.getMsg());
        }
        return productInfo.getProductPrice();
    }


    @GetMapping("/findById/{id}")
    public ProductInfo findById(@PathVariable("id")Integer id) {
        ProductInfo productInfo = productInfoService.getById(id);
        if (productInfo == null) {
            throw new ShopException(ResponseEnum.PRODUCT_NULL.getMsg());
        }
        return productInfo;
    }


    /**
     * 减库存
     *
     * @param id       商品id
     * @param quantity 购买的数量
     * @return
     */
    @PutMapping("/subStockById/{id}/{quantity}")
    public Boolean subStockById(@PathVariable("id") Integer id,@PathVariable("quantity") Integer quantity) {
        // 根据id查询出对应的商品
        ProductInfo productInfo = this.productInfoService.getById(id);
        if (productInfo == null) throw new ShopException(ResponseEnum.PRODUCT_NULL.getMsg());
        // 这个商品的库存，减掉我买的几个库存
        Integer quantity1 = productInfo.getProductStock() - quantity;
        if (quantity1 < 0) throw new ShopException(ResponseEnum.PRODUCT_STOCK_ERROR.getMsg());
        // 修改库存
        productInfo.setProductStock(quantity1);
        return this.productInfoService.updateById(productInfo);
    }


}

